;; Copyright (c) 2022 bobwxc@yeah.net
;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Lesser General Public License as published by the Free Software
;; Foundation, either version 3 of the License, or (at your option) any later
;; version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <https://www.gnu.org/licenses/>.

(in-package #:simple-tar)

(defmacro test-func (func args rst)
  "example: (test-func func (args) rst)"
  `(let ((rrst (,func ,@args)))
     (if (equal rrst ,rst)
         (format nil "Pass: ~S~%" (function ,func))
       (format nil
               "Fail: ~S~%      Args: ~S~%      Expect: ~S~%      But get: ~S~%"
               (function ,func)
               (quote ,args)
               ,rst
               rrst))))

(defparameter decoded-test-file
  '((NAME "rr百度翻译.txt" MODE "0000644" UID 1000 GID 1001 SIZE 1119
     MTIME 1652968501 CHKSUM 6700 TYPEFLAG "0" LINKNAME "" MAGIC
     "ustar " VERSION " " UNAME "wxc" GNAME "wxc" DEVMAJOR ""
     DEVMINOR "" PREFIX "")
    230 138 165 229 145 138 229 155 158 229 189 146 10 10 226 128
    156 230 136 145 228 187 172 228 184 141 228 188 154 233 128 160
    230 136 144 229 128 146 233 128 128 226 128 156 230 152 175 76
    105 110 117 120 229 134 133 230 160 184 229 188 128 229 143 145
    231 154 132 231 172 172 228 184 128 230 157 161 232 167 132 229
    136 153 239 188 155 76 105 110 117 120 231 154 132 229 136 155
    229 167 139 228 186 186 229 146 140 228 184 187 232 166 129 229
    188 128 229 143 145 228 186 186 229 145 152 108 105 110 117 115
    116 111 114 118 97 108 100 115 228 186 178 232 135 170 229 136
    155 229 187 186 228 186 134 229 174 131 231 161 174 228 191 157
    229 174 131 229 190 151 229 136 176 233 129 181 229 174 136 227
    128 130 10 10 230 156 172 230 150 135 230 161 163 230 143 143
    232 191 176 228 186 134 232 167 132 229 136 153 229 175 185 231
    148 168 230 136 183 231 154 132 230 132 143 228 185 137 228 187
    165 229 143 138 76 105 110 117 120 229 134 133 230 160 184 231
    154 132 229 188 128 229 143 145 230 168 161 229 158 139 231 161
    174 228 191 157 229 164 132 231 144 134 230 137 128 230 156 137
    230 138 165 229 145 138 231 154 132 229 155 158 229 189 146 239
    188 155 231 155 184 229 133 179 230 150 185 233 157 162 229 175
    185 228 186 142 229 134 133 230 160 184 229 188 128 229 143 145
    228 186 186 229 145 152 230 157 165 232 175 180 229 164 132 231
    144 134 229 155 158 229 189 146 46 10 233 135 141 232 166 129
    228 189 141 239 188 136 229 143 136 229 144 141 226 128 156 84
    76 239 188 155 68 82 226 128 157 239 188 137 10 10 32 32 32 32
    229 166 130 230 158 156 229 156 168 228 184 128 228 184 170 76
    105 110 117 120 229 134 133 230 160 184 228 184 173 232 191 144
    232 161 140 232 137 175 229 165 189 231 154 132 228 184 156 232
    165 191 232 191 144 232 161 140 229 190 151 230 155 180 231 179
    159 239 188 140 232 191 153 230 152 175 228 184 128 231 167 141
    229 155 158 229 189 146 230 136 150 232 128 133 228 184 128 228
    184 170 230 155 180 230 150 176 231 154 132 231 137 136 230 156
    172 229 176 177 228 184 141 233 156 128 232 166 129 228 186 134
    227 128 130 230 179 168 230 132 143 239 188 140 229 191 133 233
    161 187 231 188 150 232 175 145 230 155 180 230 150 176 231 154
    132 229 134 133 230 160 184 228 189 191 231 148 168 231 177 187
    228 188 188 231 154 132 233 133 141 231 189 174 239 188 155 228
    184 139 233 157 162 231 154 132 232 175 166 231 187 134 232 167
    163 233 135 138 232 175 180 230 152 142 228 186 134 232 191 153
    228 184 128 231 130 185 228 187 165 229 143 138 229 133 182 228
    187 150 230 155 180 232 175 166 231 187 134 231 154 132 231 187
    134 232 138 130 227 128 130 10 10 32 32 32 32 230 138 165 229
    145 138 230 130 168 231 154 132 233 151 174 233 162 152 239 188
    140 229 166 130 228 184 173 230 137 128 232 191 176 230 138 165
    229 145 138 233 151 174 233 162 152 44 229 174 131 229 183 178
    231 187 143 230 182 181 231 155 150 228 186 134 229 155 158 229
    189 146 229 146 140 233 135 141 229 164 141 231 154 132 230 137
    128 230 156 137 233 135 141 232 166 129 230 150 185 233 157 162
    228 184 186 228 186 134 230 150 185 228 190 191 232 181 183 232
    167 129 227 128 130 229 133 182 228 184 173 230 156 137 228 184
    164 228 184 170 229 190 136 233 135 141 232 166 129 239 188 154
    229 188 128 229 167 139 228 189 160 230 138 165 229 145 138 231
    154 132 228 184 187 233 162 152 228 189 191 231 148 168 226 128
    156 91 229 155 158 229 189 146 93 226 128 157 229 185 182 230
    138 132 233 128 129 230 136 150 232 189 172 229 143 145 231 187
    153 229 155 158 229 189 146 233 130 174 228 187 182 229 136 151
    232 161 168 40 229 155 158 229 189 146 64 229 136 151 232 161
    168 46 108 105 110 117 120 231 179 187 231 187 159 46 229 188
    128 229 143 145 41 46 10 10 32 32 32 32 229 143 175 233 128 137
    239 188 140 228 189 134 229 187 186 232 174 174 239 188 154 229
    156 168 229 143 145 233 128 129 230 136 150 232 189 172 229 143
    145 230 138 165 229 145 138 230 151 182 239 188 140 229 176 134
    76 105 110 117 120 229 134 133 230 160 184 229 155 158 229 189
    146 232 183 159 232 184 170 98 111 116 226 128 156 114 101 103
    122 98 111 116 226 128 157 233 128 154 232 191 135 230 140 135
    229 174 154 229 189 147 229 155 158 229 189 146 230 152 175 232
    191 153 230 160 183 229 188 128 229 167 139 231 154 132 239 188
    154 10 10 32 32 32 32 35 114 101 103 122 98 111 116 229 188 149
    229 133 165 228 186 134 118 53 227 128 130 49 51 227 128 130
    227 128 130 231 172 172 53 231 137 136 227 128 130 49 52 45 114
    99 49 229 158 139 10 10))

(defun test ()
  (format t "~%=simple-tar test start=~%")
  (with-open-file (fp "./test.tar"
                      :direction :output
                      :element-type '(unsigned-byte 8)
                      :if-exists :rename)
                  (format t "~A" (test-func write-one-file
                                            (fp
                                             (make-tar-header :NAME "rr百度翻译.txt"
                                                              :MODE "0000644"
                                                              :UID 1000
                                                              :GID 1001
                                                              :SIZE 1119
                                                              :MTIME 1652968501
                                                              :CHKSUM 6700
                                                              :TYPEFLAG "0"
                                                              :LINKNAME ""
                                                              :MAGIC "ustar "
                                                              :VERSION " "
                                                              :UNAME "wxc"
                                                              :GNAME "wxc"
                                                              :DEVMAJOR ""
                                                              :DEVMINOR ""
                                                              :PREFIX "")
                                             (cdr decoded-test-file))
                                            +zero-512-block+)))
  (with-open-file (fp "test.tar"
                      :direction :input
                      :element-type '(unsigned-byte 8)
                      :if-does-not-exist :error)
                  (format t "~A" (test-func read-one-file (fp) decoded-test-file)))
  (with-open-file (fp "test.tar"
                      :direction :input
                      :element-type '(unsigned-byte 8)
                      :if-does-not-exist :error)
                  (format t "~A" (test-func read-next-file (fp) decoded-test-file)))
  (with-open-file (fp "test.tar"
                      :direction :input
                      :element-type '(unsigned-byte 8)
                      :if-does-not-exist :error)
                  (format t "~A" (test-func read-all-files (fp) (list decoded-test-file))))
  (with-open-file (fp "test.tar"
                      :direction :input
                      :element-type '(unsigned-byte 8)
                      :if-does-not-exist :error)
                  (delete-file fp)
                  (format t "Pass: delete test.tar~%"))
  (format t "=simple-tar test done=~%")
  )

